package com.feicheng.controller.wx;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.Afj;
import com.feicheng.persistence.entity.Amd;
import com.feicheng.persistence.entity.Ayyjlitem;
import com.feicheng.persistence.mapper.AfjMapper;
import com.feicheng.persistence.service.IAfjService;
import com.feicheng.persistence.service.IAmdService;
import com.feicheng.persistence.service.IAyyjlitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/wx/fj")
public class FjController {

    @Autowired
    IAfjService iAfjService;

    @Autowired
    IAmdService iAmdService;

    @Autowired
    IAyyjlitemService iAyyjlitemService;

    @Autowired
    AfjMapper afjMapper;

    @PostMapping("/list/get")
    public ResultInfo getFjByList(@RequestBody JSONObject param) {
        QueryWrapper<Afj> qw = new QueryWrapper<>();

        String mdid = param.getString("mdid");
        if (StringUtils.hasLength(mdid)) {
            qw.eq("mdid", mdid);
        }

        String fjmc = param.getString("fjmc");
        if (StringUtils.hasLength(fjmc)) {
            qw.like("fjmc", fjmc);
        }

        param.put("fjs", iAfjService.list(qw));
        return ResultInfo.success(param);
    }

    @PostMapping("/price/get")
    public ResultInfo getPriceByList(@RequestBody JSONObject param) {

        String fjid = param.getString("fjid");
        String st = param.getString("st");
        String et = param.getString("et");

        Afj fj = iAfjService.getById(fjid);

        JSONArray prices = new JSONArray();
        JSONObject price = new JSONObject();
        price.put("tag", "全天");
        price.put("st", st);
        price.put("et", et);
        price.put("dj", fj.getDj());
        prices.add(price);
        return ResultInfo.success(prices);
    }

    /**
     * 房间分页查询
     * @param param
     * @return
     */
    @PostMapping("/page/get")
    public ResultInfo getFjByPage(@RequestBody JSONObject param) {

        Integer current = param.getInteger("current");
        Integer size = param.getInteger("size");

        if (current == null || current < 0) {
            current = 1;
        }

        if (size == null || size < 0) {
            size = 16;
        }

        // 构建分页
        Page<JSONObject> page = new Page<JSONObject>(current, size);
        QueryWrapper<JSONObject> qwfj = new QueryWrapper<>();

        String fjid = param.getString("fjid");
        if (StringUtils.hasLength(fjid)) {
            qwfj.eq("fjid", fjid);
        }

        String fjmc = param.getString("fjmc");
        if (StringUtils.hasLength(fjmc)) {
            qwfj.like("fjmc", fjmc);
        }

        String tp = param.getString("tp");
        if (StringUtils.hasLength(tp)) {
            qwfj.eq("tp", tp);
        }

        String func = param.getString("func");
        if (StringUtils.hasLength(func)) {
            qwfj.eq("func", func);
        }

        String dzid = param.getString("dzid");
        if (StringUtils.hasLength(dzid)) {
            qwfj.eq("dzid", dzid);
        }

        String jd = param.getString("jd");
        String wd = param.getString("wd");
        String cd = param.getString("cd");
        String st = param.getString("st");
        String et = param.getString("et");

        if (StringUtils.hasLength(st)) {
            st += ":00";
        }

        if (StringUtils.hasLength(et)) {
            et += ":00";
        }

        page = afjMapper.getRoomWithStoreByPage(page, jd, wd, cd, st, et, qwfj);
        param.put("fjs", page);
        return ResultInfo.success(param);

    }

    /**
     * 房间详细查询
     * @param param
     * @return
     */
    @PostMapping("/info/get")
    public ResultInfo getFjInfo(@RequestBody JSONObject param) {

        String fjid = param.getString("fjid");
        if (!StringUtils.hasLength(fjid)) {
            throw new SystemException("id必须指定");
        }

        Afj fj = iAfjService.getById(fjid);
        param.put("fj", fj);

        Amd amd = iAmdService.getById(fj.getMdid());
        param.put("md", amd);

        QueryWrapper<Ayyjlitem> yyitqw = new QueryWrapper<>();
        yyitqw.eq("fjid", fjid);
        param.put("yyyjlitems", iAyyjlitemService.list(yyitqw));

        return ResultInfo.success(param);
    }

}
